package frame.model;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.sql.*;

/**
 * @author 王迎澳
 * @version 1.0
 * @school hhu
 * @date 2022/5/30 20:08
 */
public class Utils {

    public static String passwordMD5(String str) {
        return Hashing.md5().newHasher().putString(str, Charsets.UTF_8).hash().toString();
    }

    /**
     * 验证表中的某个键是否重复
     * @param table 表名
     * @param key 键名
     * @param value 值名
     * @return true:  表示新的值和表中的数据有重复
     *         false: 该数据没有重复，可以使用
     */
    public static boolean varify(String table, String key, String value) {

        Connection con = null;
        ResultSet resultSet = null;
        try {
            Class.forName("org.opengauss.Driver");
            con = DriverManager.getConnection("jdbc:opengauss://121.36.41.207:26000/finance", "geqiyu", "Tlyz0729");

            PreparedStatement preparedStatement2 = con.prepareStatement("SET SEARCH_PATH TO finance,public;");
            preparedStatement2.execute();
            preparedStatement2.close();

            Statement statement = con.createStatement();
            resultSet = statement.executeQuery("select count(*) from " + table + " where " + key + " = " + value);
//            if(resultSet.isBeforeFirst()==false) return false;
            resultSet.next();
            String string = resultSet.getString(1);
            statement.close();

            if ("1".equals(string.trim())) {
                return true;
            }else{
                return false;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


}
